﻿
-- ================================================
-- Remarks: 
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Ivy_UserRole_Insert')
	BEGIN
		DROP  Procedure  Ivy_UserRole_Insert
	END

GO

CREATE PROCEDURE [Ivy_UserRole_Insert] 
-- =============================================
-- Author:		<Rampravesh and Sagar>
-- Create date: <14/10/2011>
-- Description:	<Ivy_UserRole_Insert>
-- =============================================

-- Add the parameters for the stored procedure here
	(
				@UserKey int = NULL,
				@UserSocietyKey int = NULL,
				@UserRoleKey int = NULL,
				
				@Sys_CreatedByID varchar(50)= NULL,
				@Sys_DeleteType int = 0,
				@Sys_OrganizationID int = NULL,
				@Sys_InitialPrimaryKeys varchar(max)= NULL,
				@Sys_WorkstationName varchar(50)= NULL,
				
				@Sys_DateOfCreation datetime OUTPUT
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT OFF;

	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END
	DECLARE @CurrentDataTime datetime;
	SET @CurrentDataTime = GETDATE();
	
    INSERT INTO [UserRole]
			(
				[UserKey],
				[UserSocietyKey],
				[UserRoleKey],
				[UserLoginID],
				[UserSocietyCode],
				[UserSocietyName],
				[UserRoleCode],
				[UserRoleName],
				[Sys_DateOfCreation],
				[Sys_DateOfModification],
				[Sys_CreatedByID],
				[Sys_ModifiedByID],
				[Sys_DeleteType],
				[Sys_IsDeleted],
				[Sys_OrganizationID],
				[Sys_InitialPrimaryKeys],
				[Sys_WorkstationName]
			)
     VALUES
           (
				@UserKey,
				@UserSocietyKey,
				@UserRoleKey,
				(SELECT [UserLoginID] FROM [User] WHERE [UserKey] = @UserKey),
				(SELECT [SocietyCode] FROM [Society] WHERE [SocietyKey] = @UserSocietyKey),
				(SELECT [SocietyName] FROM [Society] WHERE [SocietyKey] = @UserSocietyKey),
				(SELECT [RoleCode] FROM [Sys_RoleMaster] WHERE [RoleKey] = @UserRoleKey),
				(SELECT [RoleName] FROM [Sys_RoleMaster] WHERE [RoleKey] = @UserRoleKey),
				@CurrentDataTime,
				@CurrentDataTime,
				@Sys_CreatedByID,
				@Sys_CreatedByID,
				@Sys_DeleteType,
				0,
				@Sys_OrganizationID,
				@Sys_InitialPrimaryKeys,
				@Sys_WorkstationName
			);
	SET @UserKey = SCOPE_IDENTITY();
	SET @Sys_DateOfCreation = @CurrentDataTime;
END
GO
